package system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.controller.base.BaseController;
import common.model.po.SysTopic;
import common.model.po.SysTopicComment;
import common.model.po.SysTopicCommentLike;
import common.model.po.SysTopicLike;
import common.model.request.base.PaginationRequest;
import common.model.response.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import system.model.request.*;
import system.model.vo.SysTopicCommentVo;
import system.model.vo.SysTopicVo;
import system.service.SysTopicCommentLikeService;
import system.service.SysTopicCommentService;
import system.service.SysTopicLikeService;
import system.service.SysTopicService;

import javax.annotation.Resource;

/**
 * 前端控制器
 *
 * @author 米泽鹏
 * @since 2023-08-29 上午 12:15
 */
@Api(tags = "话题管理")
@RestController
@RequestMapping("/system/sys-topic")
public class SysTopicController extends BaseController {

	@Resource
	private SysTopicService topicService;
	@Resource
	private SysTopicCommentService commentService;
	@Resource
	private SysTopicLikeService topicLikeService;
	@Resource
	private SysTopicCommentLikeService commentLikeService;

	@ApiOperation(value = "话题分页查询", notes = "话题分页查询")
	@PostMapping("/readOnly/pageSysTopic")
	public Result<Page<SysTopicVo>> pageSysTopic(@ApiParam(value = "话题分页查询请求", required = true) @RequestBody @Validated PaginationRequest<SearchTopicRequest, SysTopic> paginationRequest) {
		return successWithData(topicService.pageSysTopic(paginationRequest));
	}

	@ApiOperation(value = "话题新增或更新", notes = "话题新增或更新")
	@PostMapping("/saveTopic")
	public Result<?> saveTopic(@ApiParam(value = "话题新增或更新请求", required = true) @RequestBody @Validated SaveTopicRequest request) {
		return topicService.saveTopic(request);
	}

	@ApiOperation(value = "话题删除", notes = "话题删除")
	@DeleteMapping("/deleteTopic/{id}")
	public Result<?> deleteTopic(@ApiParam(value = "话题id", required = true) @PathVariable Long id) {
		return topicService.deleteTopic(id);
	}

	@ApiOperation(value = "关闭或开启话题", notes = "关闭或开启话题")
	@PutMapping("/toggleTopicClosed")
	public Result<?> toggleTopicClosed(@ApiParam(value = "关闭或开启话题请求", required = true) @RequestBody @Validated ToggleTopicClosedRequest request) {
		return topicService.toggleTopicClosed(request);
	}

	@ApiOperation(value = "修改话题排序", notes = "修改话题排序")
	@PutMapping("/modifyTopicSort")
	public Result<?> modifyTopicSort(@ApiParam(value = "修改话题排序请求", required = true) @RequestBody @Validated ModifyTopicSortRequest request) {
		return topicService.modifyTopicSort(request);
	}

	@ApiOperation(value = "修改评论排序", notes = "修改评论排序")
	@PutMapping("/modifyTopicCommentSort")
	public Result<?> modifyTopicCommentSort(@ApiParam(value = "修改评论排序请求", required = true) @RequestBody @Validated ModifyTopicCommentSortRequest request) {
		return commentService.modifyTopicCommentSort(request);
	}

	@ApiOperation(value = "评论分页查询", notes = "评论分页查询")
	@PostMapping("/readOnly/pageTopicComment")
	public Result<Page<SysTopicCommentVo>> pageTopicComment(@ApiParam(value = "评论分页查询请求", required = true) @RequestBody @Validated PaginationRequest<SearchTopicCommentRequest, SysTopicComment> paginationRequest) {
		return successWithData(commentService.pageTopicComment(paginationRequest));
	}

	@ApiOperation(value = "评论新增或更新", notes = "评论新增或更新")
	@PostMapping("/saveTopicComment")
	public Result<?> saveTopicComment(@ApiParam(value = "评论新增或更新请求", required = true) @RequestBody @Validated SaveTopicCommentRequest request) {
		return commentService.saveTopicComment(request);
	}

	@ApiOperation(value = "评论删除", notes = "评论删除")
	@DeleteMapping("/deleteTopicComment/{id}")
	public Result<?> deleteTopicComment(@ApiParam(value = "评论id", required = true) @PathVariable Long id) {
		return commentService.deleteTopicComment(id);
	}

	@ApiOperation(value = "点赞话题", notes = "点赞话题")
	@PostMapping("/likeTopic")
	public Result<?> likeTopic(@ApiParam(value = "点赞话题请求", required = true) @RequestBody @Validated LikeTopicRequest request) {
		return topicLikeService.likeTopic(request);
	}

	@ApiOperation(value = "取消点赞话题", notes = "取消点赞话题")
	@PostMapping("/cancelLikeTopic")
	public Result<?> cancelLikeTopic(@ApiParam(value = "取消点赞话题请求", required = true) @RequestBody @Validated LikeTopicRequest request) {
		return topicLikeService.cancelLikeTopic(request);
	}

	@ApiOperation(value = "点赞评论", notes = "点赞评论")
	@PostMapping("/likeComment")
	public Result<?> likeComment(@ApiParam(value = "点赞评论请求", required = true) @RequestBody @Validated LikeCommentRequest request) {
		return commentLikeService.likeComment(request);
	}

	@ApiOperation(value = "取消点赞评论", notes = "取消点赞评论")
	@PostMapping("/cancelLikeComment")
	public Result<?> cancelLikeComment(@ApiParam(value = "取消点赞评论请求", required = true) @RequestBody @Validated LikeCommentRequest request) {
		return commentLikeService.cancelLikeComment(request);
	}

	@ApiOperation(value = "获取话题的点赞数", notes = "获取话题的点赞数")
	@GetMapping("/readOnly/getLikeCountByTopicId/{topicId}")
	public Result<?> getLikeCountByTopicId(@ApiParam(value = "话题id", required = true) @PathVariable Long topicId) {
		return Result.successWithData(topicLikeService.lambdaQuery().eq(SysTopicLike::getTopicId, topicId).count());
	}

	@ApiOperation(value = "获取评论的点赞数", notes = "获取评论的点赞数")
	@GetMapping("/readOnly/getLikeCountByCommentId/{commentId}")
	public Result<?> getLikeCountByCommentId(@ApiParam(value = "评论id", required = true) @PathVariable Long commentId) {
		return Result.successWithData(commentLikeService.lambdaQuery().eq(SysTopicCommentLike::getTopicCommentId, commentId).count());
	}

}
